package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.operator.*;
import com.ejlchina.searcher.util.MapUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.ServiceException;
import com.sky.mapper.CategoryMapper;
import com.sky.service.CategoryService;
import com.sky.utils.BeanCopyUtil;
import com.sky.utils.DateUtils;
import com.sky.web.page.TableSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BeanSearcher beanSearcher;

    @Override
    public SearchResult<Category> selectCategoryList(CategoryDTO dto) {
        Map<String, Object> build = MapUtils.builder()
                .page(TableSupport.buildPageRequest().getPageNum(),
                        TableSupport.buildPageRequest().getPageSize())
                .field(Category::getName,dto.getName()).op(Contain.class)
                .field(Category::getType,dto.getType()).op(Equal.class)
                .field(Category::getStatus,dto.getStatus()).op(Equal.class)
                .build();
        return beanSearcher.search(Category.class, build);
    }

    /**
     * 分类类别查询全部
     * @param dto 查询信息
     * @return
     */
    @Override
    public List<Category> selectCategoryListByType(CategoryDTO dto) {
        Map<String, Object> build = MapUtils.builder()
                .field(Category::getType,dto.getType()).op(Equal.class)
                .field(Category::getStatus,dto.getStatus()).op(Equal.class)
                .build();
        return beanSearcher.searchAll(Category.class, build);
    }



    /**
     * 根据id查询分类信息
     * @param id 分类id
     * @return
     */
    @Override
    public Category selectCategoryById(Long id) {
        Map<String, Object> build = MapUtils.builder()
                .field(Category::getId,id).op(Equal.class)
                .build();
        return beanSearcher.searchFirst(Category.class, build);
    }

    /**
     * 新增分类
     * @param dto 分类信息
     */
    @Override
    public int insertCategory(CategoryDTO dto) {
        Category category = new Category();
        //属性拷贝
        BeanCopyUtil.copyProperties(dto, category);
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        //设置创建时间、修改时间、创建人、修改人
        category.setCreateTime(DateUtils.getNowDate());
        category.setUpdateTime(DateUtils.getNowDate());
        category.setCreateBy(BaseContext.getCurrentName());
        category.setUpdateBy(BaseContext.getCurrentName());
        return categoryMapper.insert(category);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public int updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //设置修改时间、修改人
        category.setUpdateTime(DateUtils.getNowDate());
        category.setUpdateBy(BaseContext.getCurrentName());
        return categoryMapper.updateById(category);
    }

    /**
     * 根据id删除分类
     * @param ids id集合
     */
    public int deleteCategoryByIds(Long[] ids) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Map<String, Object> build = MapUtils.builder()
                .field(Dish::getCategoryId,Arrays.asList(ids)).op(InList.class)
                .build();

        int count = beanSearcher.searchCount(Dish.class, build).intValue();
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new ServiceException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        Map<String, Object> build1 = MapUtils.builder()
                .field(Setmeal::getCategoryId,Arrays.asList(ids)).op(Equal.class)
                .build();
        count = beanSearcher.searchCount(Setmeal.class, build1).intValue();
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new ServiceException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除分类数据
        UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
        wrapper.in("id",(Object[]) ids);
        wrapper.set("del_flag","2");
        wrapper.set("update_time", DateUtils.getNowDate());
        wrapper.set("update_by", BaseContext.getCurrentName());
        return categoryMapper.update(null,wrapper);
    }
}
